package com.eqdd.yiqidian.ui.mine;

import android.databinding.DataBindingUtil;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.adapter.slimadapter.SlimAdapterEx;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.kyleduo.switchbutton.SwitchButton;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;

/**
 * @author吕志豪 .
 * @date 17-10-26  下午3:52.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class FriendCircleSettingActivity extends BaseActivity {

    private RecyclerViewCustom dataBinding;
    private SlimAdapterEx<Item65> slimAdapterEx;
    private String friendGuid;

    @Override
    public void initBinding() {

        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "工作圈权限设置");
    }

    @Override
    public void initData() {

        friendGuid = getIntent().getStringExtra("friendGuid");
        dataBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        slimAdapterEx = SlimAdapterEx.create().register(R.layout.list_item_65, new SlimInjector<Item65>() {
            @Override
            public void onInject(Item65 data, IViewInjector injector) {

                injector.text(R.id.tv_left_center_content, data.getTilte())
                        .text(R.id.tv_bottom, data.getTip())
                        .with(R.id.sb_end, view -> {
                            ((SwitchButton) view).setChecked(data.isOpen());
                            ((SwitchButton) view).setOnCheckedChangeListener((view1, isChecked) -> {
                                int position = slimAdapterEx.getData().indexOf(data);
                                if (position == 0) {
                                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + (isChecked ? HttpConfig.OPEN_SEE_1 : HttpConfig.CLOSE_SEE_1))
                                            .params("userGuid", user.getGuid())
                                            .params("friendGuid", friendGuid)
                                            .execute(new DialogCallBack<HttpResult>(FriendCircleSettingActivity.this) {
                                                @Override
                                                public void onSuccess(Response<HttpResult> response) {
                                                    HttpResult httpResult = response.body();
                                                    ToastUtil.showShort(httpResult.getMsg());
                                                    if (httpResult.getStatus() == 200) {
                                                        slimAdapterEx.getDataItem(position).setOpen(isChecked);
                                                    }
                                                }
                                            });
                                } else {
                                    OkGo.<HttpResult>post(HttpConfig.BASE_URL + (isChecked ? HttpConfig.OPEN_SEE_2 : HttpConfig.CLOSE_SEE_2))
                                            .params("userGuid", user.getGuid())
                                            .params("friendGuid", friendGuid)
                                            .execute(new DialogCallBack<HttpResult>(FriendCircleSettingActivity.this) {
                                                @Override
                                                public void onSuccess(Response<HttpResult> response) {
                                                    HttpResult httpResult = response.body();
                                                    ToastUtil.showShort(httpResult.getMsg());
                                                    if (httpResult.getStatus() == 200) {
                                                        slimAdapterEx.getDataItem(position).setOpen(isChecked);
                                                    }
                                                }
                                            });
                                }
                            });
                        });

            }
        }).attachTo(dataBinding.recyclerView).updateData(new ArrayList());
    }

    @Override
    public void setView() {

        ArrayList<Item65> data = new ArrayList<>();
        OkGo.<HttpResult<FriendSettingInfo>>post(HttpConfig.BASE_URL + HttpConfig.FRIEND_SETTING_INFO)
                .params("userGuid", user.getGuid())
                .params("friendGuid", friendGuid)
                .execute(new DialogCallBack<HttpResult<FriendSettingInfo>>(FriendCircleSettingActivity.this) {
                    @Override
                    public void onSuccess(Response<HttpResult<FriendSettingInfo>> response) {
                        HttpResult<FriendSettingInfo> httpResult = response.body();
                        ToastUtil.showShort(httpResult.getMsg());
                        if (httpResult.getStatus() == 200) {
                            data.add(new Item65("不让对方看我的工作圈", httpResult.getItems().isNotAllToSee(), "打开后,对方无法看到你在工作圈发的动态"));
                            data.add(new Item65("不看对方的工作圈", httpResult.getItems().isNotSee(), "打开后,你将无法看到对方在工作圈发的动态"));
                            slimAdapterEx.updateData(data);
                        }
                    }
                });
    }

    public class Item65 {
        String tilte;
        boolean isOpen;
        String tip;

        public Item65(String tilte, boolean isOpen, String tip) {
            this.tilte = tilte;
            this.isOpen = isOpen;
            this.tip = tip;
        }

        public String getTilte() {
            return tilte;
        }

        public void setTilte(String tilte) {
            this.tilte = tilte;
        }

        public boolean isOpen() {
            return isOpen;
        }

        public void setOpen(boolean open) {
            isOpen = open;
        }

        public String getTip() {
            return tip;
        }

        public void setTip(String tip) {
            this.tip = tip;
        }
    }

    public class FriendSettingInfo {

        /**
         * NotSee : false
         * NotAllToSee : false
         */

        private boolean NotSee;
        private boolean NotAllToSee;

        public boolean isNotSee() {
            return NotSee;
        }

        public void setNotSee(boolean NotSee) {
            this.NotSee = NotSee;
        }

        public boolean isNotAllToSee() {
            return NotAllToSee;
        }

        public void setNotAllToSee(boolean NotAllToSee) {
            this.NotAllToSee = NotAllToSee;
        }
    }
}
